package com.wedo.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class WedoCorsConfiguration {
    @Bean
    public CorsFilter corsFilter() {

        // 初始cors配置对象
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许的域,不要写*，否则cookie就无法使用了
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedOrigin("http://zpf.free.idcfengye.com");
        // 允许携带Cookie信息
        corsConfiguration.setAllowCredentials(true);
        // 允许的请求方式, * 代表允许所有请求方式s
        corsConfiguration.addAllowedMethod("*");
        // 允许的头信息, * 代表所有的请求方法
        corsConfiguration.addAllowedHeader("*");

        // 初始化cors配置源对象
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**",corsConfiguration);

        // 返回CorsFilter实例, 参数: cors配置源对象
        return new CorsFilter(configurationSource);
    }
}
